import { Component, ComponentFactoryResolver, Inject, Injectable, Injector, TemplateRef, Type } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { Subject, Subscription } from 'rxjs';
import { SearchDrawerComponent } from './search-drawer.component';

@Injectable({
  providedIn: 'root'
})
export class SearchDrawerService {
  closeEvent = new Subject<any>();
  createEvent = new Subject<any>();
  initEvent = new Subject<any>();

  visible = false;

  drawer: Subscription[] = [];
  constructor(private drawerService: NzDrawerService) {}

  create(defaultValue: Record<string, any>, newSchema?: SFSchema, newUI?: SFUISchema) {
    this.createEvent.next({ defaultValue, newSchema, newUI });
    return this;
  }

  unsubscribe() {
    this.drawer.forEach(sub => sub.unsubscribe());
    this.drawer = [];
  }

  created(defaultValue: any, schema: SFSchema, instance: any) {
    const drawer = this.drawerService.create({
      nzTitle: '筛选',
      nzContent: SearchDrawerComponent,
      nzContentParams: { schema, defaultValue, instance },
      // nzMaskClosable: false,
      nzWidth: 640
    });

    return drawer;
  }
}
